package com.ruoyi.company.controller;

import java.util.List;

import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.house.domain.XlkjHouse;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.utils.DictStrUtils;
import com.ruoyi.utils.UrlUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.company.domain.XlkjCompany;
import com.ruoyi.company.service.IXlkjCompanyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 单位Controller
 *
 * @author wang
 * @date 2022-01-11
 */
@RestController
@RequestMapping("/company/company")
public class XlkjCompanyController extends BaseController
{
    @Autowired
    private IXlkjCompanyService xlkjCompanyService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private SysDeptMapper sysDeptMapper;

    /**
     * 查询单位列表
     */
    @PreAuthorize("@ss.hasPermi('company:company:list')")
    @GetMapping("/list")
    public TableDataInfo list(XlkjCompany xlkjCompany)
    {
        //筛选已通过的
        xlkjCompany.setStatus("2");
        startPage();
        if(xlkjCompany.getDepartmentId() != null && xlkjCompany.getDepartmentId() != ""){
            List<String> list = sysDeptMapper.selectIdByParentId(Long.valueOf(xlkjCompany.getDepartmentId()));
            String str = "(";
            for(int i =0;i<list.size();i++){
                str += list.get(i);
                if(i != list.size()-1){
                    str += ",";
                }
            }
            str += ")";
            xlkjCompany.setDepartmentList(str);
        }
        List<XlkjCompany> list = xlkjCompanyService.selectXlkjCompanyList(xlkjCompany);
        for(XlkjCompany house:list) {
            //部门值翻译
            String deptName = sysDeptService.selectDeptById(Long.valueOf(house.getDepartmentId())).getDeptName();
            house.setDepartmentId(deptName);
        }
        return getDataTable(list);
    }

    /**
     * 导出单位列表
     */
    @PreAuthorize("@ss.hasPermi('company:company:export')")
    @Log(title = "单位", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(XlkjCompany xlkjCompany)
    {
        List<XlkjCompany> list = xlkjCompanyService.selectXlkjCompanyList(xlkjCompany);
        for(XlkjCompany company:list){
            company.setCompanyType(DictUtils.getDictLabel(DictStrUtils.COMPANY_TYPE,company.getCompanyType()));
            company.setEconomicsType(DictUtils.getDictLabel(DictStrUtils.ECONOMICS_TYPE,company.getEconomicsType()));
            company.setIndustryType(DictUtils.getDictLabel(DictStrUtils.INDUSTRY_TYPE,company.getIndustryType()));
        }
        ExcelUtil<XlkjCompany> util = new ExcelUtil<XlkjCompany>(XlkjCompany.class);
        return util.exportExcel(list, "单位数据");
    }

    /**
     * 单位导入
     */
    @Log(title = "单位", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<XlkjCompany> util = new ExcelUtil<XlkjCompany>(XlkjCompany.class);
        List<XlkjCompany> userList = util.importExcel(file.getInputStream());
        for(XlkjCompany company:userList){
            //字典类型翻译
            company.setCompanyType(DictUtils.getDictValue(DictStrUtils.COMPANY_TYPE,company.getCompanyType()));
            company.setEconomicsType(DictUtils.getDictValue(DictStrUtils.ECONOMICS_TYPE,company.getEconomicsType()));
            company.setIndustryType(DictUtils.getDictValue(DictStrUtils.INDUSTRY_TYPE,company.getIndustryType()));
        }
        String message = xlkjCompanyService.importDevice(userList);
        return AjaxResult.success(message);
    }

    /**
     * 下载单位导入模板
     */
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<XlkjCompany> util = new ExcelUtil<XlkjCompany>(XlkjCompany.class);
        return util.importTemplateExcel("单位数据");
    }

    /**
     * 获取单位详细信息
     */
    @PreAuthorize("@ss.hasPermi('company:company:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(xlkjCompanyService.selectXlkjCompanyById(id));
    }

    /**
     * 新增单位
     */
    @PreAuthorize("@ss.hasPermi('company:company:add')")
    @Log(title = "单位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody XlkjCompany xlkjCompany)
    {
        return toAjax(xlkjCompanyService.insertXlkjCompany(xlkjCompany));
    }

    /**
     * 修改单位
     */
    @PreAuthorize("@ss.hasPermi('company:company:edit')")
    @Log(title = "单位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody XlkjCompany xlkjCompany)
    {
        return toAjax(xlkjCompanyService.updateXlkjCompany(xlkjCompany));
    }

    /**
     * 删除单位
     */
    @PreAuthorize("@ss.hasPermi('company:company:remove')")
    @Log(title = "单位", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(xlkjCompanyService.deleteXlkjCompanyByIds(ids));
    }
}
